<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.ArrayList"%>
<%@page import="model.Usuario"%>
<%@page import="java.util.List"%>
<%@page import="dao.UsuarioDAO"%>
<%
   String roles = (String)session.getAttribute("roles");
   if(roles==null){
       response.sendRedirect("/SICOM_CCI"); 
   } else {
       session.setAttribute("roles", roles);
   }
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:include page="../../_MaintLayout.html"/>
    <div class="container bs-docs-container">
        <h3>Administraci&oacute;n Usuarios</h3>
        <br/>
        <div class="alert alert-danger">
            <button type="button" class="closeAlert btn-link" aria-hidden="true">&times;</button>
            <div id="mensaje"></div>
        </div>
        <div class="alert alert-success oculto">
            <button type="button" class="closeAlert btn-link" aria-hidden="true">&times;</button>
            <div id="mensaje"></div>
        </div>
        <button class="btn btn-success btn-sm" id="addUser">
            <span class="ui-icon ui-icon-circle-plus"></span>
            <span class="buttonText">Agregar usuario</span>
        </button>
        <br/><br/>
        <div class="table-responsive">
            <table class="table table-hover table-bordered">
                <thead>
                    <tr class="info">
                        <th>Usuario</th>
                        <th>Nombre</th>
                        <th>Apellido</th>
                        <th>Tel&eacute;fono</th>
                        <th>Correo</th>
                        <th>Modificaci&oacute;n</th>
                        <th>&nbsp;</th>
                        <th style="display: none;"></th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        UsuarioDAO usuarioDaoManager = new UsuarioDAO();
                        List<Usuario> usuarios = new ArrayList<Usuario>();
                        try {
                            usuarios = usuarioDaoManager.obtenerUsuarios();
                        } catch (Exception e) { }
                        for(Usuario usr : usuarios) {
                            %>
                            <tr class="data" id="<%out.print(usr.getIdUsuario()); %>" >
                                <td><%out.print(usr.getNomUsuario()); %></td>
                                <td><%out.print(usr.getNombreUsuario()); %></td>
                                <td><%out.print(usr.getApelUsuario()); %></td>
                                <td><%if(usr.getTelUsuario()!=null)out.print(usr.getTelUsuario()); %></td>
                                <td><%if(usr.getCorreoUsuario()!=null)out.print(usr.getCorreoUsuario()); %></td>
                                <td><%
                                    if(usr.getFecModifUsuario()!=null){
                                        SimpleDateFormat dt = new SimpleDateFormat("dd MMM yyyy");
                                        out.print(dt.format(usr.getFecModifUsuario())); 
                                    }
                                %></td>
                                <td id="">
                                   <span class="deleteData ui-icon ui-icon-trash" title="Borrar"></span>
                                   <span class="editData ui-icon ui-icon-pencil" title="Editar"></span>
                                </td>
                                <td style="display: none;"><%out.print(usr.getIdUsuario()); %></td>
                            </tr>
                            <%
                        }
                    %>
                </tbody>
            </table>
        </div>
    </div>
<jsp:include page="../../_FooterLayout.html"/>

<script type="text/javascript">
    $('#addUser').click(function (event) {
        event.preventDefault();
        location.href = 'updOrAddUsuario.jsp?idUsr=0';
    });
    $('.editData').click(function (event) {
        event.preventDefault();
        location.href = 'updOrAddUsuario.jsp?idUsr=' + $(this).parent().parent().attr('id');
    });
    $('.deleteData').click(function (event) {
        event.preventDefault();
        var idUsuario = $(this).parent().parent().attr('id');
        ajaxPost("../../controller/UsuarioController.jsp","deleteUsr="+idUsuario, "json", "deleteUsr");
        $(this).parents('tr').remove();
    });
</script>